package com.yolib.sanyi.pic;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.yolib.sanyi.tool.YoliBLog;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.util.LruCache;

public class FileCache {

	private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M

	private File mCacheDir;
//	private final LruCache<String, Long> sFileCache; 
	private static FileCache mFileCache;

	public FileCache(Context context)
	{
		mCacheDir = context.getExternalCacheDir();
//		sFileCache = new LruCache<String, Long>(MAX_CACHE_SIZE){
//			@Override
//			public int sizeOf(String key, Long value){
//				return value.intValue();
//			}
//			@Override
//			protected void entryRemoved(boolean evicted, String key, Long oldValue, Long newValue){
//				File file;
//				try {
//					file = getFile(key);
//					if(file != null)
//						file.delete();
//				} catch (FileNotFoundException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//
//			}
//		};
	}
	public static Context mContext;
	public static FileCache getInstance(Context context)
	{
		mContext = context;
		if(mFileCache == null)
			mFileCache = new FileCache(context);
		return mFileCache;
	}

	private File getFile(String fileName) throws FileNotFoundException {
		YoliBLog.e("get File start");

		String mDirPath = fileName.substring(0, fileName.indexOf("/"));
		String mFileName = fileName.substring(fileName.indexOf("/")+1);
		YoliBLog.e("mFile?");
		File file = new File(mCacheDir+"/"+ mDirPath, mFileName);
		if(!file.exists() || !file.isFile())
			{
				YoliBLog.e("mFile");
				throw new FileNotFoundException(mCacheDir +"/"+ mDirPath + mFileName+" is not exist or this type is not file");
			}
		return file;
	}

	
	public boolean putBitmap(String key, Bitmap bitmap){
		File file;
		try {
			file = getFile(key);
			if(file != null){
				return true;
			}
		} catch (FileNotFoundException e) {
			try {
				String mDirPath = key.substring(0, key.indexOf("/"));
				String mFileName = key.substring(key.indexOf("/")+1);
				File mDir = new File(mCacheDir+"/"+mDirPath);
				if(!mDir.exists())
					mDir.mkdir();
				File outputfile = new File(mCacheDir+"/"+mDirPath, mFileName);
				BufferedOutputStream bos = new BufferedOutputStream
				(new FileOutputStream(outputfile));
				boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 90, bos);
				bos.flush();
				bos.close();
				
				YoliBLog.e("FILE SAVE HERE: "+outputfile.getPath());
				if(saved){
//					synchronized(sFileCache){
//						sFileCache.put(key, getFile(key).length());
//					}
					return true; 
				}
			} catch (IOException e1) {
				YoliBLog.e("File SAVE ERROR");
				e1.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			YoliBLog.e("ERROR2");
			e.printStackTrace();
		}
		return false;
	}
	//?��?key?��?OutputStream
//	private FileOutputStream getOutputStream(File file){
//		return new Fi
//		if(mCacheDir == null)
//			return null;
//		FileOutputStream fos;
//		try {
//			fos = new FileOutputStream(mCacheDir.getAbsolutePath() + File.separator + key);
//			return fos;
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return null;
//		}

//	}

	
	public Bitmap getBitmap(String key){
		File bitmapFile;
		try {
			bitmapFile = getFile(key);
			if(bitmapFile != null){
				Bitmap bitmap;
				
				BitmapFactory.Options opt=new BitmapFactory.Options(); 
	        	opt.inJustDecodeBounds = true; //設�?BitmapFactory.decodeStream�?ecode�??????????????度�?寬度	            
	            opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
	            opt.inPurgeable  = true;
	            opt.inInputShareable = true;
//	            opt.inSampleSize = ImageOptions.initSampSize(opt, width, height);//ImageHandler.computeSampleSize(opt, -1, 128*128); //�???��???��?�大�???��?OutOfMenery
	            opt.inJustDecodeBounds = false;//設�?BitmapFactory.decodeStream??ecodeFile
				
				bitmap = BitmapFactory.decodeFile(bitmapFile.getPath(), opt);//.decodeStream(new FileInputStream(bitmapFile), null, sBitmapOptions);
				
				if(bitmap != null){
					//???�??�???�硬�??�?					MemoryCache.getInstance().putBitmap(key, bitmap);
					return bitmap;
				}
				return null;
			}
			return null;
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
	}
}
